//api访问基地址
//module.exports.apiUrl = "http://api.cwq.com/api";
//module.exports.apiUrl = "http://192.168.1.209:9003/api";
module.exports.apiUrl = "https://api.banmachewu.com/cwqapi/api";
//module.exports.apiUrl = "https://dev.banmachewu.com/cwqapi/api";
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()//
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
module.exports.formatTime=formatTime
//获取UserId
module.exports.getUserId = function () {
  var value = wx.getStorageSync("userId");
  if (value) {
    return value;
  }
  else {
    return "";
  }
},
  //设置UserId
module.exports.setUserId = function (value) {
    wx.setStorageSync("userId", value);
},
/**
 * 判断用户是否登录
 */
module.exports.isUserLogin = function (value) {
    return this.getUserId()!="";
},
  //是否为手机号码
module.exports.isMobile = function (input) {
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(input)) {
      return false;
    } else {
      return true;
    }
};
/**
 * 检查用户是否登录
 * cbSuccess，登录后回调
 * cbFail,未登录回调
 */
module.exports.checkLogin=function(cbSuccess,cbFail){
    var that=this;
    var userId=that.getUserId();
    if(userId){
      cbSuccess && cbSuccess();
    }
    else{
        cbFail && cbFail();
    }
};

/**
 *安全打开某个页面
 *url须为绝对地址
 */
module.exports.safeOpenUrl = function (url, cbSuccess) {
  var that = this;
  var userId = that.getUserId();
  if (userId) {
   wx.navigateTo({
     url: url,
     success:cbSuccess
   })
  }
  else {
    wx.navigateTo({
      url: '/pages/my/login/login',
    })
  }
};
/**
 * 安全地做某个操作，
 */
module.exports.safeDoAction = function (action) {
  var that = this;
  var userId = that.getUserId();
  if (!userId) {
    wx.navigateTo({
      url: '/pages/my/login/login',
    });   
  }
  else{
    action && action();
  }
};

//异步获取数据 
//wxUrl：请求数据的地址
//wxData：请求参数
//wxCallBack：数据请求成功后的回调函数
module.exports.getData=function (wxUrl, wxData, wxSuccess, wxFail,wxComplete) {
  let that = this;
  let userId = that.getUserId();
  wx.request({
    url: wxUrl, //仅为示例，并非真实的接口地址
    data: wxData,
    header: {
      'content-type': 'application/json', // 默认值    
      "userid": userId
    },
    success: function (res) {
      if (res.statusCode == '401') {
      
      } else {
        wxSuccess && wxSuccess(res.data);
      }
    },
    fail: function () {
      wxFail && wxFail();
    },
    complete:function(){
      wxComplete && wxComplete();
    }
  })
}
module.exports.postData=function (wxUrl, wxData, wxSuccess, wxFail,wxComplete) {
  let that = this;
  let userId = that.getUserId();
  wx.request({
    url: wxUrl, //仅为示例，并非真实的接口地址
    data: wxData,
    method: 'POST',
    header: {
      'content-type': 'application/json', // 默认值  
      "userid": userId
    },
    success: function (res) {
      if (res.statusCode == 401) {
       
      } else {
        wxSuccess && wxSuccess(res.data);
      }
    },
    fail: function () {
      wxFail && wxFail();
    },
    complete:function(){
      wxComplete && wxComplete();
    }
  })
}

